package com.newegg.research;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.commons.io.FileUtils;

import com.newegg.research.tfidfAnalyzer.Indexer;

public class Name2CategoryMapping
{
	Logger logger = Logger.getLogger(Name2CategoryMapping.class.getName());
	
	private Indexer indexer = new Indexer();
	
	private Map<String, String> brandMapping = new HashMap<String, String>();
	
	public void init(String trainingFile, String brandMappingFile) throws IOException
	{
		long start = System.currentTimeMillis();

		if (brandMappingFile != null)
		{
			File brand = new File(brandMappingFile);
			Iterator<String> it = FileUtils.lineIterator(brand);
			while (it != null && it.hasNext())
			{
				String line = it.next();
				String[] param = line.split(",");
				if (param.length == 2)
				{
					brandMapping.put(param[0].toLowerCase(), param[1]
							.toLowerCase());
					// if (param[1].length() > 1 &&
					// !param[1].matches("[\\w|\\d]+"))
					// brandMapping.put(param[0].toLowerCase(), param[1]
					// .toLowerCase().substring(0, 2));
					// else
					// brandMapping.put(param[0].toLowerCase(), param[1]
					// .toLowerCase());
				}
			}
			logger.info("索引商品中英文品牌映射共" + brandMapping.size() + "条");
		}
		
		File[] files = new File(trainingFile).listFiles();
		for (File f : files)
		{
			List<String> fileContent = FileUtils.readLines(f);
			if (fileContent.size() < 20)
				continue;
			else
			{
				String category = f.getName();
				for (String line : fileContent)
				{
					if (!line.contains(category))
					{
						line += category;
					}
					String[] tokens = Util.participle(line, brandMapping);
					for (String t : tokens)
					{
						if (t.length() > 1)
							indexer.index(t, category);
					}
				}
			}
		}
		logger.info("The " + files.length + " files has been indexed");
		logger.info("共索引：" + indexer.getAllTokenNum() + "条数据");
		logger.info("总类别:" + indexer.getAllCategoriesAsMap().size() + "个");
		logger.info("构造索引共耗时:" + (System.currentTimeMillis() - start) + " ms");
	}
	
	public static void main(String[] args)
	{
		
	}
}
